package cn.alexy.dbbench.server.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Aspect 
public class DBBenchAop {
	private static Logger logger = LoggerFactory.getLogger(DBBenchAop.class);
	
	@Around("@annotation(cn.alexy.dbbench.server.service.Record)") //声明环绕通知
	public Object doAround(ProceedingJoinPoint pjp)throws Throwable

	{

	System.out.print("开始执行：");
	System.out.println(pjp.getSignature().getName());

	Date t1 = new Date();
	Object o =  pjp.proceed();
	System.out.println(pjp.getTarget().getClass().getName());
	((DBBenchService) pjp.getTarget()).getResult();
	Date t2 = new Date();
	
	System.out.print("执行结束。耗时（毫秒）：");
	System.out.println(t2.getTime() - t1.getTime());
	
	logger.info(pjp.getSignature().getName()+"执行耗时："+(t2.getTime() - t1.getTime()+"ms."));	
	
	if (pjp.getSignature().getName().equals("fillData")){ 
			((DBBenchService) pjp.getTarget()).getResult().setAddtime(t2.getTime() - t1.getTime());
	}
	
	if (pjp.getSignature().getName().equals("editData")){ 
		((DBBenchService) pjp.getTarget()).getResult().setEdittime(t2.getTime() - t1.getTime());
	}
	
	if (pjp.getSignature().getName().equals("sumData")){ 
		((DBBenchService) pjp.getTarget()).getResult().setSumtime(((DBBenchService) pjp.getTarget()).getResult().getSumtime() + t2.getTime() - t1.getTime());
		
	}
	
	if (pjp.getSignature().getName().equals("delData")){ 
		((DBBenchService) pjp.getTarget()).getResult().setDeltime(t2.getTime() - t1.getTime());
	}
	
	if (pjp.getSignature().getName().equals("queryData")){ 
		((DBBenchService) pjp.getTarget()).getResult().setQuerytime(t2.getTime() - t1.getTime());
	}
	
	if (pjp.getSignature().getName().equals("emptyData")){ 
		((DBBenchService) pjp.getTarget()).getResult().setEmptytime(t2.getTime() - t1.getTime());
	}


	return o;

	}




}
